/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//asdd
package testeinterface;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author ra120212
 */

public class PRequisito extends JPanel{
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */

        
        
        public PRequisito(){            
            this.setLayout(new GridLayout(5,2));
            
            JLabel lbCodigo        = new JLabel("Código");
            JLabel lbCertificado        = new JLabel("Certificado");
            JLabel lbCreditos           = new JLabel("Créditos necessários");
            JLabel lbTipo               = new JLabel("Tipo");
            final JTextField tfCodigo   = new JTextField("");
            final JTextField tfCertificado    = new JTextField("");
            final JTextField tfCreditos       = new JTextField("");
            final JTextField tfTipo           = new JTextField("");     
            JButton btInserir           = new JButton("Inserir");
            
            btInserir.addActionListener (new ActionListener () {  
            @Override
                 public void  actionPerformed(ActionEvent e){ 
                     
                     Logger.gravar(Logger.INICIALIZANDO_METODO, getNomeClasse() + ".actionPerformed(ActionEvent e)");
                     
                      try{
                        String host = "jdbc:mysql://143.106.150.216:3306/SatodaDB?zeroDateTimeBehavior=convertToNull";
                        String uName = "satoda";
                        String uPass = "adotas2014";
                        Connection con = DriverManager.getConnection(host, uName, uPass);
                        
                        Statement stmt = con.createStatement( );
                        //tem que mudar isso aqui pq eu nao qual o tipo deles
                        String SQL = "INSERT INTO T_REQUISITO(codigo, certificado, tipo, Créditos) VALUES ('"+tfCodigo.getText()+"', '"+ tfCertificado.getText() +"', "+tfTipo.getText()+", "+tfCreditos.getText()+")";
                        stmt.executeUpdate( SQL );
                    }
                    catch( SQLException err ){
                        Logger.gravar(Logger.ERRO, getNomeClasse() + ".actionPerformed(ActionEvent e): " + err.getMessage());
                        System.out.println(err.getMessage());
                    }
                      
                    Logger.gravar(Logger.FINALIZANDO_METODO, getNomeClasse() + ".actionPerformed(ActionEvent e)");
                    System.out.println("Requisito cadastrado com sucesso!");  
                 }  
              }); 
            this.add(lbCodigo);
            this.add(tfCodigo );
            
            this.add(lbCertificado);
            this.add(tfCertificado);
            
            this.add(lbCreditos);
            this.add(tfCreditos);
            
            this.add(lbTipo);
            this.add(tfTipo);
            this.add(btInserir);
            
            
        }
        
        private String getNomeClasse(){
            return this.getClass().getName();
        }
 
}

